import cacheKey from "./cacheType";

/**
 * 设置客户端缓存
 * @param {key} 缓存的key
 * @param {val} 缓存的val
 */
const setCaChe = (key, val) => {
  let type = typeof val;
  let cacheVal = {
    key,
    val,
    type,
    time: new Date().getTime()
  };
  uni.setStorageSync(key, cacheVal);
};

/**
 * 根据key取得客户端缓存
 * @param {key} 缓存key
 */
const getCache = key => {
  let cacheVal = uni.getStorageSync(key);
  if (cacheVal && cacheVal.val) {
    return cacheVal.val;
  }
  return null;
};

/**
 * 取得自定义导航缓存
 */
const getDiyTabbarCaChe = () => getCache(cacheKey.cacheKey.DIYTABBAR);

export default {
  setCaChe,
  getCache,
  getDiyTabbarCaChe
};
